package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.constant.basic.FundModeEnum;
import com.ssy.lingxi.common.constant.settle.SettlementOrderTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.request.CorporateAccountDetailRequest;
import com.ssy.lingxi.dto.response.CorporateAccountDetailResponse;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberInnerControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignIdVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignRegisterQueryVO;
import com.ssy.lingxi.settle.accounts.entity.CorporateAccountConfig;
import com.ssy.lingxi.settle.accounts.model.constant.PlatformSettlementOrderTypeEnum;
import com.ssy.lingxi.settle.accounts.model.constant.ReceiptInvoicePayStatusEnum;
import com.ssy.lingxi.settle.accounts.model.constant.ReceiptInvoiceStatusEnum;
import com.ssy.lingxi.settle.accounts.model.constant.SettlementStatusEnum;
import com.ssy.lingxi.settle.accounts.model.vo.response.DropdownItemVO;
import com.ssy.lingxi.settle.accounts.repository.CorporateAccountConfigRepository;
import com.ssy.lingxi.settle.accounts.repository.PlatformAccountConfigRepository;
import com.ssy.lingxi.settle.accounts.service.ICommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用接口实现
 * @author 伍将
 * @date 2020/11/2
 * @version 2.0.0
 */
@Service
public class CommonServiceImpl implements ICommonService {

    @Resource
    CorporateAccountConfigRepository corporateAccountConfigRepository;
    @Resource
    private PlatformAccountConfigRepository platformAccountConfigRepository;
    @Resource
    private MemberInnerControllerFeign memberInnerControllerFeign;

    /**
     * 获取结算单据类型
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getSettlementOrderType() {

        List<DropdownItemVO> resultList = new ArrayList<>();

        DropdownItemVO itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.ALL.getCode());
        itemVO.setText(SettlementOrderTypeEnum.ALL.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.PRODUCTION_NOTICE.getCode());
        itemVO.setText(SettlementOrderTypeEnum.PRODUCTION_NOTICE.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.LOGISTICS.getCode());
        itemVO.setText(SettlementOrderTypeEnum.LOGISTICS.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.ORDER.getCode());
        itemVO.setText(SettlementOrderTypeEnum.ORDER.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.APPLY_AMOUNT.getCode());
        itemVO.setText(SettlementOrderTypeEnum.APPLY_AMOUNT.getMessage());
        resultList.add(itemVO);

        return Wrapper.success(resultList);
    }

    /**
     * 获取策略下结算单据类型
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getStrategySettlementOrderType() {

        List<DropdownItemVO> resultList = new ArrayList<>();

        DropdownItemVO itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.PRODUCTION_NOTICE.getCode());
        itemVO.setText(SettlementOrderTypeEnum.PRODUCTION_NOTICE.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.LOGISTICS.getCode());
        itemVO.setText(SettlementOrderTypeEnum.LOGISTICS.getMessage());
        resultList.add(itemVO);

        itemVO = new DropdownItemVO();
        itemVO.setId(SettlementOrderTypeEnum.ORDER.getCode());
        itemVO.setText(SettlementOrderTypeEnum.ORDER.getMessage());
        resultList.add(itemVO);

        return Wrapper.success(resultList);
    }

    /**
     * 获取平台策略下结算单据类型
     * @author 伍将
     * @date 2020/11/5
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List<com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getPlatformStrategySettlementOrderType() {

        List<DropdownItemVO> resultList = Arrays.stream(PlatformSettlementOrderTypeEnum.values()).filter(f -> !f.getCode().equals(0)).map(map -> {
            DropdownItemVO item = new DropdownItemVO();
            item.setId(map.getCode());
            item.setText(map.getMessage());
            return item;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 获取结算状态
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getSettlementStatus() {

        List<DropdownItemVO> resultList = Arrays.stream(SettlementStatusEnum.values()).map(r -> {
            DropdownItemVO status = new DropdownItemVO();
            status.setId(r.getCode());
            status.setText(r.getMessage());
            return status;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 获取开票状态
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getReceiptInvoiceStatus() {

        List<DropdownItemVO> resultList = Arrays.stream(ReceiptInvoiceStatusEnum.values()).map(r -> {
            DropdownItemVO status = new DropdownItemVO();
            status.setId(r.getCode());
            status.setText(r.getMessage());
            return status;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 获取支付状态
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.DropdownItemVO>>
     **/
    @Override
    public Wrapper<List<DropdownItemVO>> getPayStatus() {

        List<DropdownItemVO> resultList = Arrays.stream(ReceiptInvoicePayStatusEnum.values()).map(r -> {
            DropdownItemVO status = new DropdownItemVO();
            status.setId(r.getCode());
            status.setText(r.getMessage());
            return status;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    @Override
    public Wrapper<CorporateAccountDetailResponse> getCorporateAccountDetail(CorporateAccountDetailRequest request) {
        CorporateAccountDetailResponse response=new CorporateAccountDetailResponse();
        //平台
        if(FundModeEnum.PLATFORM_EXCHANGE.getCode().equals(request.getType())){
            platformAccountConfigRepository.findAll().stream().findFirst().ifPresent(platformAccountConfigDO -> BeanUtils.copyProperties(platformAccountConfigDO, response));
        }else{//会员
            if(request.getMemberId()==null||request.getRoleId()==null){
                return Wrapper.fail(ResponseCode.MARKETING_REQUEST_PARAM_ERROR);
            }
            CorporateAccountConfig memberAccount = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(request.getMemberId(), request.getRoleId());
            if(memberAccount!=null){
                BeanUtils.copyProperties(memberAccount,response);
                // 取最新的会员名称
                MemberFeignIdVO feignIdVO = new MemberFeignIdVO();
                feignIdVO.setMemberId(request.getMemberId());
                Wrapper<MemberFeignRegisterQueryVO> wrapper = memberInnerControllerFeign.getMemberRegisterInfo(feignIdVO);
                if (null != wrapper && ResponseCode.SUCCESS.getCode() == wrapper.getCode() && null != wrapper.getData()) {
                    response.setName(StringUtils.hasLength(wrapper.getData().getLegalPersonName()) ? wrapper.getData().getLegalPersonName() : wrapper.getData().getName());
                }
            }
        }
        return Wrapper.success(response);
    }
}
